package com.ruoyi.mng.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mng.domain.HzChaBusiR;
import com.ruoyi.mng.domain.HzChaChannel;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzChaChannelService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzMerBusiChaRMapper;
import com.ruoyi.mng.domain.HzMerBusiChaR;
import com.ruoyi.mng.service.IHzMerBusiChaRService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 商户业务渠道关系Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-28
 */
@Service
public class HzMerBusiChaRServiceImpl implements IHzMerBusiChaRService {
    @Autowired
    private HzMerBusiChaRMapper hzMerBusiChaRMapper;
    @Resource
    private IHzBusiService hzBusiService;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;
    @Resource
    private IHzChaChannelService hzChaChannelService;

    /**
     * 查询商户业务渠道关系
     *
     * @param rId 商户业务渠道关系主键
     * @return 商户业务渠道关系
     */
    @Override
    public HzMerBusiChaR selectHzMerBusiChaRByRId(Long rId) {
        HzMerBusiChaR hzMerBusiChaR = hzMerBusiChaRMapper.selectHzMerBusiChaRByRId(rId);
        setExtendHzMerBusiChaR(hzMerBusiChaR);
        return hzMerBusiChaR;
    }

    /**
     * 查询商户业务渠道关系列表
     *
     * @param hzMerBusiChaR 商户业务渠道关系
     * @return 商户业务渠道关系
     */
    @Override
    public List<HzMerBusiChaR> selectHzMerBusiChaRList(HzMerBusiChaR hzMerBusiChaR) {
        List<HzMerBusiChaR> hzMerBusiChaRList = hzMerBusiChaRMapper.selectHzMerBusiChaRList(hzMerBusiChaR);
        hzMerBusiChaRList.forEach(this::setExtendHzMerBusiChaR);
        return hzMerBusiChaRList;
    }

    /**
     * description :: 给HzMerBusiChaR设置扩展信息
     */
    private void setExtendHzMerBusiChaR(HzMerBusiChaR hzMerBusiChaR) {
        hzMerBusiChaR.setExtend_merchantName(hzMerMerchantService.selectHzMerMerchantByMerchantId(hzMerBusiChaR.getMerchantId()).getMerchantShortName());
        hzMerBusiChaR.setExtend_busiName(hzBusiService.selectHzBusiByBusiId(hzMerBusiChaR.getBusiId()).getBusiName());
        HzChaChannel hzChaChannel = hzChaChannelService.selectHzChaChannelByChannelId(hzMerBusiChaR.getChannelId());
        hzMerBusiChaR.setExtend_channelName(hzChaChannel.getChannelCode() + " :: " + hzChaChannel.getChannelName());
    }

    /**
     * 新增商户业务渠道关系
     *
     * @param hzMerBusiChaR 商户业务渠道关系
     * @return 结果
     */
    @Override
    public int insertHzMerBusiChaR(HzMerBusiChaR hzMerBusiChaR) {
        hzMerBusiChaR.setCreateTime(DateUtils.getNowDate());
        hzMerBusiChaR.setUpdateTime(DateUtils.getNowDate());
        return hzMerBusiChaRMapper.insertHzMerBusiChaR(hzMerBusiChaR);
    }

    /**
     * 修改商户业务渠道关系
     *
     * @param hzMerBusiChaR 商户业务渠道关系
     * @return 结果
     */
    @Override
    public int updateHzMerBusiChaR(HzMerBusiChaR hzMerBusiChaR) {
        hzMerBusiChaR.setUpdateTime(DateUtils.getNowDate());
        return hzMerBusiChaRMapper.updateHzMerBusiChaR(hzMerBusiChaR);
    }

    /**
     * 批量删除商户业务渠道关系
     *
     * @param rIds 需要删除的商户业务渠道关系主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBusiChaRByRIds(String rIds) {
        return hzMerBusiChaRMapper.deleteHzMerBusiChaRByRIds(Convert.toStrArray(rIds));
    }

    /**
     * 删除商户业务渠道关系信息
     *
     * @param rId 商户业务渠道关系主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBusiChaRByRId(Long rId) {
        return hzMerBusiChaRMapper.deleteHzMerBusiChaRByRId(rId);
    }
}
